/**
 * 线程不安全demo volatile关键字无法解决线程安全问题
 * @author brianxia
 * @version 1.0
 * @date 2021/12/9 16:37
 */
public class ThreadNotSafe {

    public static volatile int count = 0;

    public static void main(String[] args) throws InterruptedException {

        //1.创建两个线程
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                count++;
            }
        });

        //2.分别对count ++
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                count++;
            }
        });

        thread.start();
        thread2.start();
        //3.打印结果
        thread.join();
        thread2.join();
        System.out.println(count);
    }

}
